﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Trade.Journal
{
    /// <summary>
    /// TradePictureSet.xaml 的交互逻辑
    /// </summary>
    public partial class TradePictureSet : UserControl
    {


        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set { SetValue(IsEditableProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditableProperty =
            DependencyProperty.Register("IsEditable", typeof(bool), typeof(TradePictureSet), new PropertyMetadata(true));



        public TradePictureSet()
        {
            InitializeComponent();
        }

        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void AddCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var win = new AddPicWindow();

            DependencyObject tmp = this;
            do
            {
                tmp = VisualTreeHelper.GetParent(tmp);
            } while (!(tmp is Window));

            win.Owner = tmp as Window;
            if (true == win.ShowDialog())
            {
                //if (DataContext is ObservableCollection<PhotoInfo> co && win.DataContext is AddPicWindowVM v)
                if (DataContext is DealViewModel dr && dr.PhotoSource.Source is ObservableCollection<PhotoInfo> co && win.DataContext is AddPicWindowVM v)
                {
                    co.Add(new PhotoInfo { RecordId = dr.RecordId, DealId = dr.DealId, TargetCycle = v.SelectedBarType.Value, Data = v.SelectedImage });
                }
            }
        }

        private void DelCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (DataContext is DealViewModel dr && dr.PhotoSource.Source is ObservableCollection<PhotoInfo> co && ListView_Photos.SelectedItem is PhotoInfo v)
            {
                co.Remove(v);
            }
        }

        private void DeleteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = ListView_Photos != null && ListView_Photos.SelectedIndex != -1;
        }

        private void HandleDoubleClick(object sender, MouseButtonEventArgs e)
        {
            popImg.Source = (ListView_Photos.SelectedItem as PhotoInfo)?.Data; 
            popup.IsOpen = true;
            e.Handled = true;
        }

        private void popImg_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            popImg.Width = Math.Min(SystemParameters.PrimaryScreenWidth, Math.Max(0, popImg.Width + e.Delta) );
            popImg.Height = Math.Min(SystemParameters.PrimaryScreenHeight, Math.Max(0, popImg.Height + e.Delta));
        }

        private void popImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            popup.IsOpen = false;
        }
    }
}
